//
//  LoginViewController.m
//  AboutDemo
//
//  Created by bosma on 14-3-19.
//  Copyright (c) 2014年 cn.com.bosma. All rights reserved.
//

#import "LoginViewController.h"

#define api_login_validate @"https://www.oschina.net/action/api/login_validate"
@interface LoginViewController ()

@end

@implementation LoginViewController
@synthesize webView;
@synthesize txt_Name;
@synthesize txt_Pwd;
@synthesize switch_Remember;
@synthesize isPopupByNotice;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:235.0/255 green:235.0/255 blue:243.0/255 alpha:1.0];
    self.webView.backgroundColor = [UIColor colorWithRed:235.0/255 green:235.0/255 blue:243.0/255 alpha:1.0];
    
    //决定是否显示用户名以及密码
    NSString *name = [Config shareInstance].getUserName;
    NSString *pwd = [Config shareInstance].getPwd;
    if (name && ![name isEqualToString:@""]) {
        self.txt_Name.text = name;
    }
    if (pwd && ![pwd isEqualToString:@""]) {
        self.txt_Pwd.text = pwd;
    }
    
    
    NSString *html = @"<body style='background-color:#EBEBF3'>1, 您可以在 <a href='http://www.oschina.net'>http://www.oschina.net</a> 上免费注册一个账号用来登陆<p />2, 如果您的账号是使用OpenID的方式注册的，那么建议您在网页上为账号设置密码<p />3, 您可以点击 <a href='http://www.oschina.net/question/12_52232'>这里</a> 了解更多关于手机客户端登录的问题</body>";
    [self.webView setDelegate:self];
    [self.webView loadHTMLString:html baseURL:nil];
    self.webView.hidden = NO;

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)even
{
    [self.view endEditing:YES];

}

//取消第一响应
- (void)textEnd:(id)sender
{
    [sender resignFirstResponder];
}

- (IBAction)LoginSliderAction:(id)sender {
    //处理是否记住用户名或者密码
    if (self.switch_Remember.isOn)
    {
        [[Config shareInstance] saveUserNameAndPwd:self.txt_Name.text andPwd:self.txt_Pwd.text];
    }
    //否则需要清空用户名于密码
    else
    {
        [[Config shareInstance] saveUserNameAndPwd:@"" andPwd:@""];
    }
}


- (void)click_Login:(id)sender{
    [self LoginSliderAction:nil];
}


#pragma mark - 浏览器链接处理
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    [[UIApplication sharedApplication] openURL:request.URL];
    if ([request.URL.absoluteString isEqualToString:@"about:blank"])
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
